/* 
 * 山阁网 页面数据服务
 */
(function($) {
    $(document).ready(function() {
        wosah.initBtnsClickEvent();
        wosah.readerArt.loadFirstArts();
    });


    var wosah = {
        initBtnsClickEvent: function() {
            $("#sg_nav_refesh").click(function() {
                kz.sg.ui.wosah.refresh();
            });

            $("#sg_nav_share").click(function() {
                wosah.writeArt.show();
                //为 文字 和 链接 按钮 加入 单击 事件
                $(".sg_wA_btn_text").click(function() {
                    wosah.writeArt.addTextDiv();
                });
                $(".sg_wA_btn_link").click(function() {
                    wosah.writeArt.addLinkDiv();
                });

                $(".sg_wA_btn_share").click(function() {
                    wosah.writeArt.share();
                });
                $(".sg_wA_btn_cancle").click(function() {
                    wosah.writeArt.remove();
                });
            });

        },
    };

    wosah.constVar = {
        LogonUser_ID: "#sg_const_uid",
        c_rA_template: ".sgtmpl_rA",
        partText: ".part_txt",
        partImg: ".part_img",
        getlogonUserId: function() {
            return $("#sg_const_uid").val();
        }
    }

    wosah.tools = {
        fmtDate: function(dt) {
            alert(dt.toLocaleDateString() + " " + dt.toLocaleTimeString());
            return dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
        }
    }

    //浏览文章
    wosah.readerArt = {
        loadFirstArts: function() {
            var uid = wosah.constVar.getlogonUserId();
            var kr = kz.sg.datasvc.wosah.loadFirstFuns(uid);
            if (!kr.successed) {
                alert(kr.msg);
            } else {
                wosah.readerArt.pushFunsToContainer(kr.data, true);
            }
        },
        pushFunsToContainer: function(arts, append) {
            if (arts !== null) {
                for (var i = 0; i < arts.length; i++) {
                    var art = arts[i];
                    var artDiv = $('#sgtmpl_rA > div.sgpn_rA').clone(false);

                    //写作者                    

                    artDiv.find("div.author .authorName").html(art.author.name);
                    artDiv.find("div.author .atTime").html("分享-" + art.atTimeStr);
                    artDiv.find("div.author .headimg").attr("src", art.author.picUrl);
                    //写内容
                    var ps = art.content;
                    var partDiv = artDiv.find(".part");

                    if (art.text !== "") {
                        var partTextDiv = $('#sgtmpl_rA > div.part_txt').clone(false);
                        partTextDiv.find("[role='txt']").html(art.text);
                        partDiv.append(partTextDiv);
                    }
                    if (art.resLink !== "") {
                        var partImgDiv = $('#sgtmpl_rA > div.part_img').clone(false);
                        partImgDiv.find("img").attr("src", art.resLink);
                        partDiv.append(partImgDiv.html());
                    }
                    if (append) {
                        $('#container').append(artDiv);
                    } else {
                        $('#container').prepend(artDiv);
                    }
                    wosah.readerArt.resizePartDiv(partDiv.parent());
                }
            }
        },
        resizePartDiv: function(partParentDiv) {
            var self = this;
            var partDiv = partParentDiv.find(".part");
            var b = this._calcPartDivResized(partParentDiv);
            if (b) {
                partParentDiv.find(".openBtn").click(function() {
                    self._showPartDiv(partDiv);
                    partParentDiv.find(".closeBtn").show();
                    $(this).hide();
                }).show();
                partParentDiv.find(".closeBtn").click(function() {
                    self._hidePartDiv(partDiv);
                    partParentDiv.find(".openBtn").show();
                    $(this).hide();
                });
                self._hidePartDiv(partDiv);
            }
        },
        _calcPartDivResized: function(partParentDiv) {
            //判定是否显示展开按钮  1有两张以上图片；2txt的div总高度大于168px
            var txtH = 0;
            var imgC = 0;
            partParentDiv.find("[role='txt']").each(function(index, e) {
                txtH += $(e).height();
            });
            partParentDiv.find("[role='img']").each(function(index, e) {
                imgC++;
            });

            return (imgC > 1) || (txtH > 168);
        },
        _hidePartDiv: function(partDiv) {
            partDiv.find(".pizza_tx").each(function(index, e) {
                if (index === 0) {
                    $(e).addClass("pizzaSmHeigh");
                } else {
                    $(e).hide();
                }
            });
            partDiv.find(".pizza_im").each(function(index, e) {
                if (index !== 0) {
                    $(e).hide();
                }
            });
        },
        _showPartDiv: function(partDiv) {
            partDiv.find(".pizza_tx").each(function(index, e) {
                if (index === 0) {
                    $(e).removeClass("pizzaSmHeigh");
                }
                $(e).show();
            });
            partDiv.find(".pizza_im").each(function(index, e) {
                $(e).show();
            });
        }
    }

    wosah.writeArt = {
        show: function() {
            var wADiv = jQuery('#sgtmpl_wA');
            $('.sg_wA').html(wADiv.html());
        },
        remove: function() {
            $('.sg_wA').html("");
        },
        share: function() {
            var fun = {};
            $('.sg_wA .sg_wA_part_container .part').each(function(index, e) {
                var text = $(e).find(".tx").val().replace(/\n/g, '<br />');

                var role = $(e).attr("role");

                if (role === "text") {
                    fun.text = text;
                } else {
                    fun.resLink = text;
                }
            });

            fun.authorId = $("#sg_const_uid").val();
            var kr=kz.sg.datasvc.wosah.savefun(fun);
            if(kr.successed){
                wosah.readerArt.pushFunsToContainer([kr.data], false);
            }else{
                alert(kr.msg);
            }
        },
        addTextDiv: function() {
            var txtDiv = jQuery('#sgtmpl_wA_text > div.part');
            $('.sg_wA .sg_wA_part_container').append(txtDiv.parent().html());
        },
        addLinkDiv: function() {
            var txtDiv = $('#sgtmpl_wA_link > div.part');
            $('.sg_wA .sg_wA_part_container').append(txtDiv.parent().html());
        }
    };

})(jQuery);


